package com.tangkun.environment.adapter;

/**
 * Created by tangkun on 2017/6/30.
 */

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.tangkun.environment.R;
import com.tangkun.environment.activity.ImageActivity;
import com.tangkun.environment.network.GlobalUrl;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 智能预警首页轮播图片的适配器
 */
public class RollviewpagerAdapter extends StaticPagerAdapter {

    private List<String> urlList = new ArrayList<String>();
    private Context context;

    public RollviewpagerAdapter(Context context, List<String> urlList) {
        this.context = context;
        this.urlList = urlList;
    }

    @Override
    public View getView(ViewGroup container, final int position) {
        ImageView imageView = new ImageView(container.getContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //轮播图网络地址
        String imageUrl = urlList.get(position);
        Glide.with(context)
                .load(GlobalUrl.IMG_URL + imageUrl)
                .placeholder(R.mipmap.ic_banner_error)
                .centerCrop()
                .dontAnimate()
                .into(imageView);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        //轮播图点击事件
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(view.getContext(), ImageActivity.class);
                i.putExtra("urlList", (Serializable) urlList);//图片地址集合
                i.putExtra("index", position);//索引
                view.getContext().startActivity(i);
            }
        });
        return imageView;
    }


    @Override
    public int getCount() {
        return urlList.size();
    }
}